home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Voyeur 1.1.1 / Voyeur ƒ / v code ƒ / v find.c < prev    next >
Text File  |  1994-02-27  |  5KB  |  207 lines

  1. /**********************************************************************\
  2.  
  3. File:        v find.c
  4.  
  5. Purpose:    This module handles finding text in a fork, and then
  6.             finding it again.
  7.  
  8.  
  9. Voyeur -- a no-frills file viewer
  10. Copyright ©1993-4, Mark Pilgrim
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "v find.h"
  30. #include "v.h"
  31. #include "program globals.h"
  32. #include "v hex.h"
  33. #include "v window maintenance.h"
  34. #include "v file management.h"
  35. #include "v error.h"
  36. #include "v progress.h"
  37. #include "msg graphics.h"
  38. #include "msg dialogs.h"
  39. #include "msg prefs.h"
  40. #include "util.h"
  41.  
  42. #define PROGRESS_DELAY 120
  43. #define PROGRESS_COUNT 40
  44.  
  45. Str255            gFindString;
  46. Str255            gFindHexString;
  47. Str255            gFindHexStringInAscii;
  48. int                gBufferOffset[MAX_WINDOWS];
  49. Boolean            gFindHex;
  50.  
  51. Boolean DoFindAgain(void)
  52. {
  53.     int                charnum;
  54.     unsigned long    tempoff;
  55.     Boolean            found;
  56.     int                bufferoff;
  57.     Str255            theFindStr;
  58.     unsigned long    curTicks,startTicks;
  59.     Boolean            notDoneYet;
  60.     Boolean            progressBarShowing;
  61.     int                progressCount;
  62.     
  63.     tempoff=gTheOffset[gWhichFile][gWhichFork[gWhichFile]]+gBufferOffset[gWhichFile];
  64.     bufferoff=gBufferOffset[gWhichFile];
  65.     charnum=1;
  66.     found=FALSE;
  67.     Mymemcpy(theFindStr, gFindHex ? gFindHexStringInAscii : gFindString, 256);
  68.  
  69.     progressBarShowing=FALSE;
  70.     notDoneYet=TRUE;
  71.     startTicks=TickCount();
  72.     while ((notDoneYet) && (!found) &&
  73.         (tempoff<gForkLength[gWhichFile][gWhichFork[gWhichFile]]))
  74.     {
  75.         curTicks=TickCount();
  76.         if ((!progressBarShowing) && (curTicks-startTicks>PROGRESS_DELAY))
  77.         {
  78.             OpenProgressDialog(gForkLength[gWhichFile][gWhichFork[gWhichFile]],
  79.                 gFindHex ? "\pFind hex data" : "\pFind text");
  80.             SetProgressText("\pSearching for ", gFindHex ? "\p$" : "\p“",
  81.                 gFindHex ? gFindHexString : gFindString, gFindHex ? "\p..." : "\p”...");
  82.             progressCount=0;
  83.             progressBarShowing=TRUE;
  84.         }
  85.         
  86.         if (gTheBuffer[gWhichFile][bufferoff]==(theFindStr[charnum]))
  87.         {
  88.             charnum++;
  89.             if (charnum>theFindStr[0])
  90.                 found=TRUE;
  91.         }
  92.         else charnum=1;
  93.         
  94.         if (!found)
  95.         {
  96.             tempoff++;
  97.             bufferoff++;
  98.             if (bufferoff==256)
  99.             {
  100.                 GetBuffer(gTheRefNum[gWhichFile], tempoff, gTheBuffer[gWhichFile]);
  101.                 bufferoff=0;
  102.                 if (progressBarShowing)
  103.                 {
  104.                     progressCount++;
  105.                     if (progressCount==PROGRESS_COUNT)
  106.                     {
  107.                         UpdateProgressDialog(tempoff);
  108.                         notDoneYet=DealWithOtherPeople();
  109.                         progressCount=0;
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.     }
  115.     
  116.     if (progressBarShowing)
  117.         DismissProgressDialog();
  118.     
  119.     if (found)
  120.     {
  121.         gBufferOffset[gWhichFile]=bufferoff;
  122.         if (tempoff%256)
  123.             gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=(tempoff/256)*256;
  124.         else
  125.             gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=tempoff;
  126.         GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  127.         UpdateProgramWindow(gWhichFile, TRUE);
  128.         return TRUE;
  129.     }
  130.     else if (notDoneYet)
  131.     {
  132.         HandleError(gFindHex ? hexNotFoundErr : asciiNotFoundErr);
  133.         GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  134.         return FALSE;
  135.     }
  136. }
  137.  
  138. Boolean DoFind(Boolean isHex)
  139. {
  140.     DialogPtr    theDlog;
  141.     int            itemSelected;
  142.     Handle        itemH;
  143.     short        item,itemType;
  144.     Rect        box;
  145.     
  146.     PositionDialog('DLOG', isHex ? findHexDialog : findDialog);
  147.     theDlog = GetNewDialog(isHex ? findHexDialog : findDialog, 0L, (WindowPtr)-1L);
  148.  
  149.     GetDItem(theDlog, 5, &itemType, &itemH, &box);
  150.     InsetRect(&box, -4, -4);
  151.     SetDItem(theDlog, 5, itemType, (Handle)OutlineDefaultButton, &box);
  152.     
  153.     GetDItem(theDlog,4,&itemType,&itemH,&box);
  154.     SetIText(itemH, isHex ? gFindHexString : gFindString);
  155.     SelIText(theDlog, 4, 0, 32767);
  156.     SetWTitle((WindowPtr)theDlog, isHex ? "\pFind hex data" : "\pFind text");
  157.     
  158.     ShowWindow(theDlog);
  159.     
  160.     itemSelected=0;
  161.     while ((itemSelected!=1) && (itemSelected!=2))
  162.     {
  163.         ModalDialog(isHex ? (ProcPtr)HexOFilter : (ProcPtr)ProcOFilter, &itemSelected);
  164.     }
  165.     
  166.     if (itemSelected==1)
  167.     {
  168.         GetDItem(theDlog,4,&itemType,&itemH,&box);
  169.         GetIText(itemH, isHex ? gFindHexString : gFindString);
  170.         if (isHex)
  171.         {
  172.             if (gFindHexString[0]==0x00)
  173.                 itemSelected=2;
  174.         }
  175.         else
  176.         {
  177.             if (gFindString[0]==0x00)
  178.                 itemSelected=2;
  179.         }
  180.     }
  181.  
  182.     HideWindow(theDlog);
  183.     DisposeDialog(theDlog);
  184.  
  185.     if ((itemSelected==1) && (isHex))
  186.     {
  187.         if (ValidHex(gFindHexString))
  188.         {
  189.             HexStringToAsciiString(gFindHexString, gFindHexStringInAscii);
  190.             gFindHex=isHex;
  191.         }
  192.         else
  193.         {
  194.             HandleError(invalidHexErr);
  195.             gFindHexString[0]=0x00;
  196.             itemSelected=2;
  197.         }
  198.     }
  199.     
  200.     if (itemSelected==1)
  201.     {
  202.         SaveThePrefs();
  203.         return DoFindAgain();
  204.     }
  205.     return FALSE;
  206. }
  207.